/*
 * fbank.com Inc.
 * Copyright (c) 2020 All Rights Reserved.
 * create by fm
 * date:2020-04-28
 */
package com.ouwen.smartpay.service.impl;

import com.fm.core.common.service.EntityServiceImpl;
import com.fm.core.utils.Strings;
import com.ouwen.smartpay.dao.OrderDetailDao;
import com.ouwen.smartpay.entity.OrderDetail;
import com.ouwen.smartpay.service.OrderDetailService;
import com.ouwen.smartpay.service.cache.CacheKeys;
import com.ouwen.smartpay.service.cache.ICacheService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * openapi_order_detail Service实现
 * <p>
 * Date: 2020-04-28 23:14:08
 *
 * @author fm
 */
@Service("orderDetailService")
public class OrderDetailServiceImpl extends EntityServiceImpl<OrderDetail, OrderDetailDao> implements OrderDetailService {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private OrderDetailDao orderDetailDao;

    @Resource
    private ICacheService localCacheService;


    @Override
    public OrderDetail findOrderDetailRequestInfoByOrderId(Long orderId, String partnerId, String subPartnerId) {

        try {

            return localCacheService.loadCache(CacheKeys.buildKey(CacheKeys.OPENAPI_REQUEST_BODY_CACHEKEY, partnerId, subPartnerId, String.valueOf(orderId)), () ->
                    getEntityDao().findOrderDetailRequestInfoByOrderId(orderId)
            );

        } catch (Exception e) {

            logger.debug("商户号【{}】子商户号【{}】订单【{}】请求体缓存不存在:", partnerId, subPartnerId, orderId);

            return null;

        }

    }

    @Override
    public OrderDetail findOrderDetailResponseInfoByOrderId(Long orderId, String partnerId, String subPartnerId) {

        try {

            return localCacheService.loadCache(CacheKeys.buildKey(CacheKeys.OPENAPI_RESPONSE_BODY_CACHEKEY, partnerId, subPartnerId, String.valueOf(orderId)), () ->
                    getEntityDao().findOrderDetailResponseInfoByOrderId(orderId)
            );

        } catch (Exception e) {

            logger.debug("商户号【{}】子商户号【{}】订单【{}】单响应体缓存不存在:", partnerId, subPartnerId, orderId);

            return null;

        }
    }

    @Override
    public void saveOrderDetailAndCache(String partnerId, String subPartnerId, OrderDetail orderDetail) {

        //保存数据
        save(orderDetail);

        if (Strings.isNoneBlank(orderDetail.getRequestInfo())) {

            //写入缓存，缓存写入失败，在数据加载的时候会进行check

            localCacheService.writeCache(CacheKeys.buildKey(CacheKeys.OPENAPI_REQUEST_BODY_CACHEKEY, partnerId, subPartnerId, String.valueOf(orderDetail.getOrderId())), orderDetail);

        }

        if (Strings.isNoneBlank(orderDetail.getResponseInfo())) {

            //写入缓存，缓存写入失败，在数据加载的时候会进行check
            localCacheService.writeCache(CacheKeys.buildKey(CacheKeys.OPENAPI_RESPONSE_BODY_CACHEKEY, partnerId, subPartnerId, String.valueOf(orderDetail.getOrderId())), orderDetail);

        }

    }

    @Override
    public void updateOrderDetail(String partnerId, String subPartnerId, OrderDetail orderDetail) {

        //更新数据
        update(orderDetail);

        //缓存失效
        if (Strings.isNoneBlank(orderDetail.getRequestInfo())) {

            localCacheService.evitCache(CacheKeys.buildKey(CacheKeys.OPENAPI_REQUEST_BODY_CACHEKEY, partnerId, subPartnerId, String.valueOf(orderDetail.getOrderId())));
        }

        if (Strings.isNoneBlank(orderDetail.getResponseInfo())) {

            localCacheService.evitCache(CacheKeys.buildKey(CacheKeys.OPENAPI_RESPONSE_BODY_CACHEKEY, partnerId, subPartnerId, String.valueOf(orderDetail.getOrderId())));

        }

    }

    @Override
    public OrderDetail findOrderDetailByOrderId(Long orderId) {

        return orderDetailDao.findOrderDetailByOrderId(orderId);
    }

    @Override
    public void updateNotifyInfo(Long id, String notifyInfo) {

    }
}
